package db;

/**
 * Created by Administrator on 2017/7/12.
 */

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

import entity.Cipai;
import entity.UserConfig;


public class CipaiDatabaseHelper {
    private static final String TABLE_NAME = "cipai";

    public static ArrayList<Cipai> getAllShowCipai() {
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME
                + " where parent_id is null order by cast( color_id as int)", null);
        return getCipaiListFromCursor(cursor);
    }

    public static ArrayList<Cipai> getAllCipaiByWordCount() {
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where parent_id is null order by wordcount desc",
                null);
        return getCipaiListFromCursor(cursor);
    }

    public static ArrayList<Cipai> getAllCipai() {
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
        return getCipaiListFromCursor(cursor);
    }

    public static Cipai getCipaiById(int id) {
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where id= " + id, null);
        ArrayList<Cipai> list = getCipaiListFromCursor(cursor);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return new Cipai();
        }
    }

    public static ArrayList<Cipai> getParentCipaiById(int id) {
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where id= " + id + "  or parent_id= " + id, null);
        return getCipaiListFromCursor(cursor);

    }

    public static String getPingzeById(int id) {
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where id= " + id, null);
        //String pingze = cursor.getString(8);
        //System.out.println(cursor.getColumnCount());
        //String test = cursor.getString(0);
        //Log.d("tag","qi shi hai shi lai le zhe li");
        //Log.d("tag",test);
        while (cursor.moveToNext()) {
            String pingze = cursor.getString(cursor.getColumnIndex("pingze"));
            return pingze;
        }
        return null;
//        return getPingzeByIdAndSB(cursor);
    }

    public static void deleteCipaiById(int id) {
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("delete * from " + TABLE_NAME + " where id= " + id, null);
    }

    private static ArrayList<Cipai> getCipaiListFromCursor(Cursor cursor) {
        ArrayList<Cipai> list = new ArrayList<Cipai>();
        while (cursor.moveToNext()) {
            Cipai cipai = new Cipai();
            cipai.setId(cursor.getInt(cursor.getColumnIndex("id")));
            cipai.setAlias(cursor.getString(cursor.getColumnIndex("alias")));
            cipai.setColor_id(cursor.getInt(cursor.getColumnIndex("color_id")));
            cipai.setEnname(cursor.getString(cursor.getColumnIndex("enname")));
            cipai.setName(cursor.getString(cursor.getColumnIndex("name")));
            cipai.setParent_id(cursor.getInt(cursor.getColumnIndex("parent_id")));
            cipai.setPingze(cursor.getString(cursor.getColumnIndex("pingze")));
            cipai.setSource(cursor.getString(cursor.getColumnIndex("source")));
            cipai.setTone_type(cursor.getInt(cursor.getColumnIndex("tone_type")));
            cipai.setWordcount(cursor.getInt(cursor.getColumnIndex("wordcount")));
            list.add(cipai);
        }
        System.out.println("myth" + list.size());
        return list;
    }

    public Cursor getAllCipaiName() {//vip 98首 普通用户30首

        //sharedPreferences = getSharePreferences("userInfo", Context.MODE_PRIVATE);  仅在activity

        SQLiteDatabase db = DBManager.getNewDatabase();
        //if(UserConfigDatabaseHelper.getVipModeByUserName() == 1) {
        if (UserConfig.getVipmode() == 1) {
            Cursor cursor = db.rawQuery("select id,name,enname,wordcount from cipai where id<99", null);
            return cursor;
        } else {
            Cursor cursor = db.rawQuery("select id,name,enname,wordcount from cipai where id<32", null);
            return cursor;
        }
    }

    public Cursor getExampleByCipai(String cipai){
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select author,text from example where ci_id in (select id from cipai where name = \""+cipai+"\")",null);
        return cursor;
    }

    public Cursor getExampleById(int id){
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cusor = db.rawQuery("select author,text from example where id = "+id,null);
        return cusor;
    }

    public Cursor getRGBByCipaiId(int id){
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select red,green,blue from color where id in (select color_id from cipai where id = \""+id+"\")",null);
        return cursor;
    }

    public Cursor getRGBByCipaiName(String name){
        SQLiteDatabase db = DBManager.getNewDatabase();
        Cursor cursor = db.rawQuery("select red,green,blue from color where id in (select color_id from cipai where name = \""+name+"\")",null);
        return cursor;
    }

}
